

//datagrid自动编辑功能
define("datagrid-autoedit",
		function(require, exports, module) {
			// datagrid扩展工具栏及默认方法
			function getToolbarUI(uiData) {
				var mygrid = uiData.mygrid;
				var exToolBar = uiData.uiOpts.toolbarEx;
				// 扩展工具栏
				var toolbarNode = [ {
					eName : 'linkbutton',
					text : '新增',
					uId : 'add',
					plain : true,
					iconCls : 'icon-add'
				}, {
					eName : 'linkbutton',
					text : '修改',
					uId : 'modify',
					plain : true,
					iconCls : 'icon-edit'
				}, {
					eName : 'linkbutton',
					text : '删除',
					uId : 'del',
					plain : true,
					iconCls : 'icon-remove'
				} ];
				toolbarNode = $ui.extUIs(toolbarNode, exToolBar);
				$.each(toolbarNode, function(i, node) {// 遍历工具栏中按钮，加入默认方法
					var row = null;
					if (!node)
						return;// 空节点退出
					if (node.eName == 'linkbutton') {
						if (node.onClick == undefined)
							node.onClick = gettbclickfn(uiData, node.uId);
					} else if (node.eName == 'searchbox') {
						if (node.searcher == null) {
							node.searcher = function() {
								mygrid.datagrid('load');
							}
						}
					}
				});
				uiData.uiOpts.toolbarEx = toolbarNode;
				return toolbarNode;
			}
			// 工具栏默认事件
			function gettbclickfn(uiData, uId) {
				var mygrid = uiData.mygrid;
				var uiOpts = uiData.uiOpts;
				var toolbarCick = {
					add : function() {// 新增
						row = {};
						if (uiOpts.onBeforeAdd) {// 返回false，则终止继续
							if (false == uiOpts.onBeforeAdd.call(mygrid.get(0),
									row)) {
								return;
							}
						}
						edit(uiData, '新增', row);
					},
					modify : function() {// 修改
						row = mygrid.datagrid('getSelected');
						if (row == null) {
							$ui.alert('请选择一条要修改的记录 !');
							return;
						}
						if (uiOpts.onBeforeModify) {// 返回false，则终止继续
							if (false == uiOpts.onBeforeModify.call(mygrid
									.get(0), row)) {
								return;
							}
						}
						edit(uiData, '修改', row);
					},
					del : function() {// 默认删除方法
						row = mygrid.datagrid('getSelected');
						if (row == null) {
							$ui.alert('请选择一条要删除的记录 !');
							return;
						}
						if (uiOpts.onBeforeDel) {// 返回false，则终止继续
							if (false == uiOpts.onBeforeDel.call(mygrid.get(0),
									row))
								return;
						}
						deleteRow(uiData, row);
					}
				};
				return toolbarCick[uId];
			}
			// 编辑表格
			function edit(uiData, title, row) {
				var mygrid = uiData.mygrid;
				var uiOpts = uiData.uiOpts;
				var myWin = null, myform = null;
				var formDialog = $.extend(true, {width : 300}, uiOpts.formDialog);
				var formEx = formDialog.formEx;
				if (formEx == undefined)return;
				delete formDialog.formEx;
				formDialog.title = title + (formDialog.title || "");
				var onFormSave = formEx.onSave;// 保存自定义的
				var formUI = createForms(uiOpts.columns);
				$.extend(formEx, {
					eName : "formEx",
					formData : row,
					onSave : function(mbean) {// 自定义表单提交
						if (mbean.result) {
							myWin.window("close");
							mygrid.datagrid("reload");
						}
						if (onFormSave)
							onFormSave.call(this, mbean);
					},
					elements : formUI
				});
				//确定窗口默认高度
				if(formDialog.height == undefined){
					formDialog.height = formUI[0].rows * 30 + 90;
				}
				var bodyUI = {
					eName : "layoutExt",
					elements : [ {
						region : "center",
						elements : formEx
					}, {
						region : "south",
						height : 50,
						cssClass : "dialog_button_layout",
						elements : [ {
							eName : 'linkbutton',
							text : '保存',
							onClick : function() {
								myform.formEx("submit");// 表单提交
							}
						},{
							eName:'span',
							cssStyle:'margin:12px;',
						}, {
							eName : 'linkbutton',
							text : '取消',
							onClick : function() {
								myWin.window("close");
							}
						} ]
					} ]
				}
				$("body").createUI($.extend(formDialog, {
					eName : "window",
					onLoad : function() {
						myWin = this;
						myform = this.findJq("formEx");
					},
					bodyUI : bodyUI
				}));
			}
			// 默认删除记录方法
			function deleteRow(uiData, row) {
				var mygrid = uiData.mygrid;
				var gridOpts = uiData.uiOpts;
				var rowKey = row[gridOpts.idField];
				if (undefined == rowKey) {// 判断是否新增行
					var index = mygrid.datagrid('getRowIndex', row);
					mygrid.datagrid('deleteRow', index);
					return;
				}
				if (undefined == gridOpts.delUrl) {
					$ui.print('delUrl is null');
					return;
				}
				$ui.confirm('删除后不能撤销,你确定要删除此记录吗 ?', function(r) {
					if (r == false)
						return;// 取消
					var idField = gridOpts.idField;// 主键字段
					var params = {};
					params[idField] = rowKey;// 主键参数
					$ui.postEx(gridOpts.delUrl, params, function(retJson) {
						if ($ui.checkret(retJson,null)) {
							var index = mygrid.datagrid('getRowIndex', row);
							mygrid.datagrid('deleteRow', index);
						} else {
							$ui.alert(retJson.info);
						}
					});
				});
			}

			// 生成表单元素
			function createForms(columns) {
				var formTable = {// 定义表单表格
					eName : 'layoutTable',
					width : '96%',
					cols : 2,
					rows : 7,
					//inputWidth : 170,
					tdWidth : [ 80, -1 ],
					trHeight:[30] //2017-05-31 10:00 Yuan 设置默认高度
				}
				// 要编辑的元素
				var editFormUI = [];
				var hiddenForm = []; // 隐藏表单
				var colSize = 0;
				// 遍历所有字段
				$.each(columns, function(i, column) {
					$.each(column, function(i, col) {// 遍历列
						if (col.hidden) {
							hiddenForm.push({
								eName : 'input',
								type : "hidden",
								name : col.field
							});
						} else {
							var tr = [];
							if (col.editor == null)// 跳出，null为不可编辑行
								return true;
							tr.push(col.title);
							var editor = $.extend({
								eName : 'textbox',// 默认编辑类型
								name : col.field
							}, col.editor);
							if(editor.cssStyle == undefined){//2017-05-31 10:00 Yuan 设置默认宽度
								editor.cssStyle = "width:97%";
							}
							tr.push(editor);
							editFormUI.push(tr);
							colSize++;
						}
					});
				});
				// 行数
				formTable.rows = colSize;
				formTable.cells = editFormUI;
				hiddenForm.unshift(formTable);
				return hiddenForm;
			}
			return function(mygrid, uiOpts) {// 工厂函数
				return getToolbarUI({
					mygrid : mygrid,
					uiOpts : uiOpts
				});
			}
});
